﻿//Type.registerNamespace("RFBR");
Type.registerNamespace("RFBR.Object");
RFBR.Object = function (settings) {
    if (!this._events) {
        this._events = [];
    }
    this._events = this._events.concat(["Inited"]);
    this._ControllerPath = null;
    this._AppPath;
    this._Waiter = this._createUpdateProgress();
    this._initProperties(settings);
    if (!window.sessionStorage) {
        window.sessionStorage = {};
    }
    this._componentReady();
};

var objectProto = RFBR.Object.prototype;

objectProto._componentReady = function () {
    this.Inited.fire(this);
};

objectProto._initProperties = function (settings) {
    var self = this;
    for (var i = 0; i < this._events.length; i++) {
        var event = this._events[i];
        this[event] = {};
        this[event].On = [];
        this[event].fire = function (sender, arguments) {
            for (var j = 0; j < this.On.length; j++) {
                if (this.On[j].func) {
                    this.On[j].func.call(this.On[j].context, sender, arguments);
                }
            }
        }
        this[event].add = function (func, context) {
            this.On.push({ func: func, context: context });
        }
        this[event].clear = function () {
            this.On = [];
        }
    }
    if (!settings) settings = {};
    for (var prop in settings) {
        if (this._events.indexOf(prop) < 0) {
            this["_" + prop] = settings[prop];
            this["get" + prop] = function () {
                return this["_" + prop];
            };
        }
        else {
            this[prop].add(settings[prop].func, settings[prop].context);
        }
    }
};

objectProto._createBlock = function (tag, classes, content, tooltip) {
    var htmlString = '<' + tag + ' class="RFBR ' + (classes ? classes : "") + '" '
		+ (tooltip ? 'title="' + tooltip + '"' : '') + '>'
		+ (content ? content : '')
		+ '</' + tag + '>';
    return $.parseHTML(htmlString)[0];
};

objectProto._createUpdateProgress = function () {
    return {
        _domNode: this._createBlock("div", "UpdateProgress"),
        _counter: 0,
        show: function () {
            if (this._counter == 0) {
                document.body.appendChild(this._domNode);
            }
            this._counter++;
        },
        hide: function () {
            if (this._counter > 0) this._counter--;
            if (this._counter == 0) {
                document.body.removeChild(this._domNode);
            }
        }
    };
};

objectProto = null;

RFBR.Object.registerClass("RFBR.Object");